package com.zznzzn.myweb.service;


import com.zznzzn.myweb.exceptions.ServiceException;
import com.zznzzn.myweb.mapper.OrderMapper;
import com.zznzzn.myweb.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public int placeOrder(Order order) {
        if (orderMapper.addOrder(order) != 0){
            return 1;
        }else{
            throw new ServiceException("创建订单失败");
        }
    }

    @Override
    public List<Order> getSellOrders(String uuid) {
        List<Order> orders = orderMapper.queryOrdersByFrom(uuid);
        if (!orders.isEmpty()) {
            return orders;
        }else{
            throw new ServiceException("没有相关订单");
        }
    }

    @Override
    public List<Order> getBuyOrders(String uuid) {
        List<Order> orders = orderMapper.queryOrdersByTo(uuid);
        if (!orders.isEmpty()) {
            return orders;
        }else{
            throw new ServiceException("没有相关订单");
        }
    }


    @Override
    public int disorder(String orderid) {
        if (orderMapper.deleteOrder(orderid) != 0){
            return 1;
        }else{
            throw new ServiceException("删除订单失败");
        }
    }

    @Override
    public int updateOrder(Order order) {
        if (orderMapper.updateOrder(order) != 0){
            return 1;
        }else{
            throw new ServiceException("更新订单失败");
        }
    }

    @Override
    public Order getOrderById(String orderid) {
        Order order = orderMapper.queryOrderById(orderid);
        if (order != null){
            return order;
        }else{
            throw new ServiceException("没有该订单");
        }
    }

    @Override
    public int getSellCount(String uuid) {
        return orderMapper.getSellCount(uuid);
    }

    @Override
    public int getBuyCount(String uuid) {
        return orderMapper.getBuyCount(uuid);
    }
}
